/*
 * Galaxium Messenger
 * 
 * Copyright (C) 2008 Philippe Durand <draekz@gmail.com>
 * 
 * License: GNU General Public License (GPL)
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

using System;
using System.IO;
using System.Net.Sockets;
using System.Text;

namespace Galaxium.Protocol.GaduGadu
{
	public class SendMessageCommand : GaduCommand
    {
        private int _recipient;
        private int _sequence = 0;
        private int _class;
        private byte[] _message;
        
        public int Recipient
        {
            set { _recipient = value; }
            get { return _recipient; }
        }
        
        public int Class
        {
            set { _class = value; }
            get { return _class; }
        }
        
        public string Message
        {
            get
            {
                Encoding enc = Encoding.Unicode;
                
                return enc.GetString(Encoding.Convert(Encoding.GetEncoding("windows-1250"), Encoding.Unicode, _message));
            }
            set
            {
                _message = new byte[value.Length + 1];
                
                Encoding enc = Encoding.GetEncoding("windows-1250");
                byte[] cp1250;
                cp1250 = enc.GetBytes(value);
                
                for (int i = 0; i < value.Length; i++)
                    _message[i] = cp1250[i];
                
                _message[value.Length] = (byte)'\0';
            }
        }
        
        public SendMessageCommand (int recipient, int sequence, int clas, string message) : base (GaduConstants.CMD_OUT_SEND_MSG)
        {
        	_recipient = recipient;
        	_sequence = sequence;
        	_class = clas;
        	Message = message;
        }
        
        protected override void Write (BinaryWriter writer)
        {
            writer.Write(_recipient);
            writer.Write(_sequence);
            writer.Write(_class);
            
            for (int i = 0; i < _message.Length; i++)
                writer.Write (_message[i]);
        }
        
        public override string ToString ()
        {
        	return String.Empty;
        }
    }
}
